package com.heg.api.util;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 消息推送平台(使用激光推送)
 * @author 邓绪招
 * @time 2016-04-04
 */
public class PushMessageUtils {
	
	private final String STR_JPUSH_APPKEY = "1a42c39be31ff3c700eddee5";
	private final String STR_JPUSH_MASTER_SECRET = "375fe1fbc8f576c47da52662";
	
	/**
	 * 单个人之间推送（点对点） Android版本
	 * @param regId 注册设备ID
	 * @param contents 推送内容
	 * @param title 标题
	 * @param badge 推送角标
	 */
	public Boolean push_to_registrationId_at_android(String regId, String contents, String title, Integer badge){
		try {
			JPushClient jPushClient = new JPushClient(STR_JPUSH_MASTER_SECRET, STR_JPUSH_APPKEY, 1);
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android())
					.setAudience(Audience.registrationId(regId)).setMessage(Message.newBuilder()
					.setMsgContent(contents).setTitle(title)
				    .setContentType("").build()).build();
			PushResult pushResult = jPushClient.sendPush(payload);
			return pushResult != null ? true : false;
		} catch (APIConnectionException e) {
			return false;
		} catch (APIRequestException e) {
			return false;
		}
	}
	
	/**
	 * 单个人之间推送（点对点） Ios版本
	 * @param regId 注册设备ID
	 * @param contents 推送内容
	 * @param title 标题
	 * @param badge 推送角标
	 */
	public Boolean push_to_registrationId_at_ios(String regId, String contents, String title, Integer badge){
		try {
			JPushClient jPushClient = new JPushClient(STR_JPUSH_MASTER_SECRET, STR_JPUSH_APPKEY, 1);
			PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.ios())
					.setAudience(Audience.registrationId(regId))
					.setNotification(Notification.newBuilder()
					.addPlatformNotification(IosNotification.newBuilder()
					.setAlert(contents).setBadge(+badge).build()).build()).build();
			PushResult pushResult = jPushClient.sendPush(payload);
			return pushResult != null ? true : false;
		} catch (APIConnectionException e) {
			return false;
		} catch (APIRequestException e) {
			return false;
		}
	}
	
	/**
	 * 向所有设备推送通知
	 * @param contents 推送内容
	 * @param title 推送标题
	 * @param badge 角标
	 */
	public Boolean push_every_all(String contents, String title, Integer badge){
		try {
			JPushClient jPushClient = new JPushClient(STR_JPUSH_MASTER_SECRET, STR_JPUSH_APPKEY, 1);
			PushPayload ios_payload = PushPayload.newBuilder().setPlatform(Platform.ios())
					.setAudience(Audience.all()).setNotification(Notification.newBuilder()
					.addPlatformNotification(IosNotification.newBuilder().setAlert(contents)
					.setBadge(+badge).build()).build()).build();
			PushPayload android_payload = PushPayload.newBuilder()
					.setPlatform(Platform.android()).setAudience(Audience.all())
					.setMessage(Message.newBuilder().setMsgContent(contents).setTitle(title).build()).build();
			PushResult ios_pushResult = jPushClient.sendPush(ios_payload);
			PushResult android_pushResult = jPushClient.sendPush(android_payload);
			return ios_pushResult != null && android_pushResult != null ? true : false;
		} catch (APIConnectionException e) {
			return false;
		} catch (APIRequestException e) {
			return false;
		}
	}
	
	/**
	 * 向指定组设备推送通知
	 * @param contents 推送内容
	 * @param title 推送标题
	 * @param tags 目标组
	 * @param badge 角标
	 */
	public Boolean push_group_to_android_and_ios(String contents, String title, String tags, Integer badge){
		JPushClient jPushClient = new JPushClient(STR_JPUSH_MASTER_SECRET, STR_JPUSH_APPKEY, 1);
		PushResult android_pushResult = null;
		PushResult ios_pushResult = null;
		try {
			PushPayload ios_payload = PushPayload.newBuilder().setPlatform(Platform.ios())
					.setAudience(Audience.tag(tags))
					.setNotification(Notification.newBuilder().addPlatformNotification(IosNotification
					.newBuilder().setAlert(contents).setBadge(+badge).build()).build()).build();  //ios
			
			ios_pushResult = jPushClient.sendPush(ios_payload);
		} catch (APIConnectionException e) {
			ios_pushResult = null;
			System.out.println("jpush plugin connect ios fail");
		} catch (APIRequestException e) {
			ios_pushResult = null;
			System.out.println("jpush plugin request ios fail");
		}
		
		try {
			PushPayload android_payload = PushPayload.newBuilder().setPlatform(Platform.android())
					.setAudience(Audience.tag(tags))
					.setMessage(Message.newBuilder().setMsgContent(contents).setTitle(title)
					.setContentType("").build()).build(); //Android
			android_pushResult = jPushClient.sendPush(android_payload);
		} catch (APIConnectionException e) {
			android_pushResult = null;
			System.out.println("jpush plugin connect android fail");
		} catch (APIRequestException e) {
			android_pushResult = null;
			System.out.println("jpush plugin request android fail");
		}
		return ios_pushResult != null || android_pushResult != null ? true : false;
	}
}
